package com.self.common.config;

import cn.hutool.core.io.FileUtil;
import com.self.common.constant.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;

/**
 * @version v1.0
 * @ClassName: ResourceServerConfig
 * @Description:
 * @Author: Mac
 * @Date: 2022/5/29 11:53
 */
@Slf4j
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()  //使用jwt，不需要csrf
                   .sessionManagement().disable() //基于token，不需要session
                   .authorizeRequests().antMatchers(
                           "/users/register",
                           "/sms/sendTo",
                           "/gt/register",
                           "/login",
                        "/v2/api-docs",
                        "/swagger-resources/configuration/ui",//用来获取支持的动作
                        "/swagger-resources",//用来获取api-docs的URI
                        "/swagger-resources/configuration/security",//安全选项
                        "/webjars/**",
                        "/swagger-ui.html"
                 ).permitAll()
                  .antMatchers("/**").authenticated()
                  .and()
                  .headers().cacheControl();//请求头不缓存

    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        //配置token存储器
        resources.tokenStore(tokenStore());
    }

    private TokenStore tokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
        ClassPathResource resource = new ClassPathResource("matchexchange.pub");
        String key = null;
        try {
            byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
            key = new String(bytes, CommonConstants.UTF8);
        } catch (IOException e) {
           log.error("读取公钥失败:{}",e);
        }
        //公钥校验
        jwtAccessTokenConverter.setVerifierKey(key);
        return jwtAccessTokenConverter;
    }
}
